package etr.bk.gui.lpan;

import etr.bk.db.WorkDBInfo;
import etr.bk.db.WrapperLPan;
import etr.bk.gui.*;
import etr.bk.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.BadLocationException;

import org.h2.jdbc.JdbcSQLException;

public class LPanel extends JPanel implements DocumentListener {
	private JList<PictPan> itemList;
	private BufferedImage img;
	private JTextField textField;
	private DefaultListModel<PictPan> defListMod;
	private Object[] defListModTemp;

	private JScrollPane scrollPanel;
	
	private RenderListPan render;
	private boolean allowSelectedFlag;
	private int tmpSelectedIndex = 0;
	private int selectedIndex = 0;

	private PictPan pictTmp;
	private PictPan pictExist;
	
	private MainListener mListener;

	public LPanel() {

		this.allowSelectedFlag = true;

		//-------tmp-pan-----------------
		pictTmp = new PictPan(-1, "", null);
		
		Dimension dim = new Dimension(230, 450);
		BorderLayout borderLayout = new BorderLayout();
		this.setPreferredSize(dim);
		this.setBorder(BorderFactory.createRaisedBevelBorder());
		this.setLayout(borderLayout);

		textField = new JTextField();

		defListMod = new DefaultListModel<PictPan>();

		render = new RenderListPan();

		itemList = new JList<PictPan>(defListMod);

		itemList.setBackground(Color.GRAY);
		itemList.setCellRenderer(render);
		itemList.setVisibleRowCount(20);
		itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		scrollPanel = new JScrollPane(itemList);
		scrollPanel.setPreferredSize(new Dimension(220, 100));

		this.add(scrollPanel, borderLayout.CENTER);

		this.textField.setEnabled(true);
		this.textField.getDocument().addDocumentListener(this);// addActionListener(this);

		this.add(textField, borderLayout.SOUTH);

		// -----------------MAIN Listener----------------------------

		this.mListener = MainListener.getListener();
		this.mListener.setLeftPanel(this);

		this.setItemList(this.mListener.getInfoForLPan()); // bad

		// -----------------inner listener-----------
		itemList.addListSelectionListener(new ListSelListen());
		// ----------------------------------------------------------
	}

	// -----------------------------------------------

	public void setItemList(ArrayList<WrapperLPan> wrapArr) {

		if (!wrapArr.isEmpty()) {

			mListener.leftPanelIsEmpty(false);
			
			this.defListMod.removeAllElements();
			for (WrapperLPan tmp : wrapArr) {
				defListMod.addElement(new PictPan(tmp.getID(), tmp.getStr(), tmp.getImg()));
			}
			
			this.setSelIndex();
			this.itemList.setSelectedIndex(this.selectedIndex);
			
			if (!this.defListMod.isEmpty()) {
				this.mListener.getInfoForRPan(this.itemList.getSelectedValue().getId()); 
			} 
			else {
				this.mListener.getInfoForRPan(null);
			}

			defListModTemp = defListMod.toArray();
		}
		else {
			this.defListMod.removeAllElements();
			this.mListener.getInfoForRPan(null);
			mListener.leftPanelIsEmpty(true);
		}
	}

	public void setSelectedModeForPictPan(boolean fl) {
		this.render.setAllowSelectedMode(fl);
		this.allowSelectedFlag = fl;
		if (fl){
			this.itemList.setSelectedIndex(this.selectedIndex);
			System.out.println("//////// seleindex"+this.selectedIndex);
		}
	}

	private void setSelIndex() {
		if (defListMod.getSize() == selectedIndex){
			this.selectedIndex = this.selectedIndex - 1;
		}
		/*if (defListMod.getSize() == tmpSelectedIndex) {
			this.selectedIndex = this.tmpSelectedIndex - 1;
		} 
		/*else {
			this.selectedIndex = this.tmpSelectedIndex;
		}*/
		this.itemList.setSelectedIndex(this.selectedIndex);
		
	}

	// --------------------------------INNER LISTENER-----

	private class ListSelListen implements ListSelectionListener {

		public void valueChanged(ListSelectionEvent arg0) {
			if (LPanel.this.allowSelectedFlag && arg0.getValueIsAdjusting()) {
				PictPan rpan = null;
				try {

					if (LPanel.this.selectedIndex != -1)
						LPanel.this.tmpSelectedIndex = LPanel.this.selectedIndex;

					LPanel.this.selectedIndex = itemList.getSelectedIndex();
					
					rpan = (PictPan) itemList.getSelectedValue();

					if (rpan == null)
						System.out.println("NULL in ListCelListener in LPanel");

					LPanel.this.mListener.getInfoForRPan(rpan.getId());

					System.out.println("***Call ListSelLister "
							+ arg0.getValueIsAdjusting());
				} catch (NullPointerException e) {
					e.getStackTrace();
					System.out.println("NULL POINTER in inf pan " + " "
							+ e.getCause());
				}
			}
		}
	}
	
	//--------------Add events-----------------------------------
	
	public void addNewEmptyItem(){
		pictTmp.setFoto(null);
		pictTmp.setText(null);
		this.defListMod.addElement(pictTmp);
	
		
		//this.setSelectedModeForPictPan(true);
		System.out.println("+++++ "+ defListMod.size());
		
		int indexTmp = defListMod.size()-1;
		this.itemList.setSelectedIndex(indexTmp);
		
		this.itemList.ensureIndexIsVisible(indexTmp);
		this.render.setAllowSelectedMode(false);
		this.render.setOneSelItem(pictTmp);
		//this.setSelectedModeForPictPan(false); // net viborki!!!!
		
		//pictTmp.setForeground(Color.RED);
		//this.itemList.setSelectedIndex(defListMod.size()-1);
	}
	
	public void selectOnEnd(){
		this.itemList.setSelectedIndex(this.defListMod.getSize()-1);
		this.mListener.getInfoForRPan(this.itemList.getSelectedValue().getId()); 
	}
	
	public void setTmpPanText(String str){
		this.pictTmp.setText(str);
		System.out.println("paintim");
		this.repaint();
	}
	
	public void setTmpPanImg(BufferedImage image){
		this.pictTmp.setFoto(image);
		this.repaint();
	}
	
	public void editExistsItem(){
		System.out.println("EDIT ITEM "+ this.selectedIndex);
		this.pictExist = defListMod.elementAt(selectedIndex);
		this.render.setAllowSelectedMode(false);
		this.render.setOneSelItem(pictExist);
	}
	
	public void setExistsPanText(String str){
		if (this.pictExist != null){
			this.pictExist.setText(str);
			System.out.println("paintim");
			this.repaint();
		}
	}
	
	public void setExistsPanImg(BufferedImage image){
		if (this.pictExist != null){
			this.pictExist.setFoto(image);
			this.repaint();
		}
	}
	
	//----------------ADD events---------------------------

	// ---------------FILTER----------------
	public void changedUpdate(DocumentEvent arg0) {

		System.out.println("change");
	}

	public void insertUpdate(DocumentEvent arg0) {

		System.out.println("insert  " + this.textField.getText());

		String strTmp = textField.getText();
		PictPan panTmp = null;

		if (!textField.getText().equals("")) {

			defListMod.removeAllElements();

			for (Object obTmp : defListModTemp) {
				panTmp = (PictPan) obTmp;

				if (panTmp.getText().contains(
						strTmp.subSequence(0, strTmp.length()))) {
					defListMod.addElement(panTmp);
				}
			}

			if (defListMod.isEmpty())
				this.mListener.getInfoForRPan(null);
			else
				itemList.setSelectedIndex(0);
		} else {
			// May be delete!!
			for (Object obTmp : defListModTemp) {
				defListMod.addElement((PictPan) obTmp);
			}
		}

	}

	public void removeUpdate(DocumentEvent arg0) {

		System.out.println("remove |" + this.textField.getText() + "|");

		String strTmp = textField.getText();
		PictPan panTmp = null;

		if (!textField.getText().equals("")) {

			defListMod.removeAllElements();

			for (Object obTmp : defListModTemp) {
				panTmp = (PictPan) obTmp;

				if (panTmp.getText().contains(
						strTmp.subSequence(0, strTmp.length()))) {
					defListMod.addElement(panTmp);
				}
			}
			if (defListMod.isEmpty())
				this.mListener.getInfoForRPan(null);
			else
				itemList.setSelectedIndex(0);
		} else {
			defListMod.removeAllElements();
			for (Object obTmp : defListModTemp) {
				defListMod.addElement((PictPan) obTmp);
			}
			itemList.setSelectedIndex(0);
		}

	}
}
